package cn.iocoder.aics.module.member.service.chathistory;

import cn.iocoder.aics.framework.common.pojo.PageResult;
import cn.iocoder.aics.framework.common.util.object.BeanUtils;
import cn.iocoder.aics.module.member.controller.admin.chathistory.vo.ChatHistoryPageReqVO;
import cn.iocoder.aics.module.member.controller.admin.chathistory.vo.ChatHistorySaveReqVO;
import cn.iocoder.aics.module.member.dal.dataobject.chathistory.ChatHistoryDO;
import cn.iocoder.aics.module.member.dal.mysql.chathistory.ChatHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

/**
 * 聊天记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ChatHistoryServiceImpl implements ChatHistoryService {

    @Resource
    private ChatHistoryMapper chatHistoryMapper;

    @Override
    public Long createChatHistory(ChatHistorySaveReqVO createReqVO) {
        // 插入
        ChatHistoryDO chatHistory = BeanUtils.toBean(createReqVO, ChatHistoryDO.class);
        chatHistoryMapper.insert(chatHistory);
        // 返回
        return chatHistory.getId();
    }

    @Override
    public void updateChatHistory(ChatHistorySaveReqVO updateReqVO) {
        // 校验存在
        validateChatHistoryExists(updateReqVO.getId());
        // 更新
        ChatHistoryDO updateObj = BeanUtils.toBean(updateReqVO, ChatHistoryDO.class);
        chatHistoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteChatHistory(Long id) {
        // 校验存在
        validateChatHistoryExists(id);
        // 删除
        chatHistoryMapper.deleteById(id);
    }

    private void validateChatHistoryExists(Long id) {
//        if (chatHistoryMapper.selectById(id) == null) {
//            throw exception(CHAT_HISTORY_NOT_EXISTS);
//        }
    }

    @Override
    public ChatHistoryDO getChatHistory(Long id) {
        return chatHistoryMapper.selectById(id);
    }

    @Override
    public PageResult<ChatHistoryDO> getChatHistoryPage(ChatHistoryPageReqVO pageReqVO) {
        return chatHistoryMapper.selectPage(pageReqVO);
    }

}